# Maintainer: Sebastian Ehlert <awvwgk@disroot.org>

_pyname=fprettify
_realname=${_pyname}
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.3.7
pkgrel=3
pkgdesc="An auto-formatter for modern Fortran code that imposes strict whitespace formatting, written in Python (mingw-w64)"
url="https://github.com/pseewald/fprettify"
license=("GPL-3.0-or-later")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-configargparse")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "unzip")
source=("${_realname}-${pkgver}.zip::https://github.com/pseewald/${_realname}/archive/v${pkgver}.zip")
sha256sums=('1dcd817ecb2cdb13ef10403810f861c79694b29d1ecab142642341c55226c076')
noextract=("${_realname}-${pkgver}.zip")
#sha256sums=('052da19a9080a6641d3202e10572cf3d978e6bcc0e7db29c1eb8ba724e89adc7')
#noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  cd ${srcdir}

#  # Symlinks require manual intervention
#  tar zxf "${srcdir}/${_realname}-${pkgver}.tar.gz" || true
#  MSYS=winsymlinks:lnk tar zxf "${srcdir}/${_realname}-${pkgver}.tar.gz"
#  for i in examples/example_before.f90 examples/example_after.f90 hook.yaml; do
#    test -f "${_realname}-${pkgver}/$i" && rm "${_realname}-${pkgver}/$i"
#  done
  unzip "${srcdir}/${_realname}-${pkgver}.zip"

  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done
}
